package logic

import (
	"context"
	"errors"
	"shop_srv/model/mysql"
	"shop_srv/model/redis"

	"shop_srv/internal/svc"
	"shop_srv/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartAddLogic {
	return &CartAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CartAddLogic) CartAdd(in *shop.CartAddRequest) (*shop.CartAddResponse, error) {
	// todo: add your logic here and delete this line
	s := mysql.Shop{}
	err := s.ShopInfo(int(in.ShopId))
	if err != nil {
		return nil, errors.New("商品不存在")
	}
	if s.Stock < int(in.Num) {
		return nil, errors.New("库存不够")
	}
	redis.CartAdd(in.UserId, in.ShopId, in.Num, s.Title, s.Cover, s.Status, s.Price)
	return &shop.CartAddResponse{}, nil
}
